package com.lvtongka.app.ui.login

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.lvtongka.app.base.BaseViewModel
import com.lvtongka.app.common.bean.AccountRequest
import com.lvtongka.app.fragment.my.MyViewModel
import com.lvtongka.app.fragment.my.bean.MyResponse
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.HttpCallBack
import com.lvtongka.app.http.OkhttpUtils
import com.lvtongka.app.http.common.UrlConstants
import com.lvtongka.app.ui.login.bean.LoginFastRequest
import com.lvtongka.app.ui.login.bean.LoginResponse
import com.lvtongka.app.ui.login.bean.SendAt
import com.lvtongka.app.ui.login.bean.SendRequest
import com.lvtongka.app.ui.login.bean.SendResponse

/**
 * describe:
 *
 * @author:ZhangWei(张伟)
 * date:2024/11/9
 * time:16:53
 */
class LoginViewModel : BaseViewModel() {

    private val _sendAt= MutableLiveData<SendAt>()
    val sendAt: LiveData<SendAt> = _sendAt

    private val _result= MutableLiveData<Boolean>()
    val result: LiveData<Boolean> = _result

    /**
     * 发送短信验证码
     */
    fun sendSmsCode(cell: String) {
        val request = SendRequest(cell)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_SEND_SMS_CODE, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    var resp = Gson().fromJson(result, SendResponse::class.java)
                    _sendAt.postValue(resp.data)
                }
            })
    }

    /**
     * 登录、注册
     */
    fun sendSmsLogin(cell: String, smsVerifyCode: String) {
        val request = SendRequest(cell, smsVerifyCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_SEND_SMS_LOGIN, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    var resp = Gson().fromJson(result, LoginResponse::class.java)
                    SPHelper.saveLoginInfo(resp.data)
                    getMyInfo()
                }
            })
    }

    fun getMyInfo() {
        val request = AccountRequest("03")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                    _result.postValue(true)
                }

                override fun onSuccess(result: String) {
                    // 存储数据
                    val my = Gson().fromJson(result, MyResponse::class.java)
                    when(my.data.cell_bind_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_CELL_BIND, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_CELL_BIND, true)
                    }
                    when(my.data.real_name_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, true)
                    }
                    when(my.data.password_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_SET_PWD, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_SET_PWD, true)
                    }
                    when(my.data.func_scan_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_FUNC_SCAN, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_FUNC_SCAN, true)
                    }
                    when(my.data.no_password_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_NO_PASSWORD, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_NO_PASSWORD, true)
                    }
                    SPHelper.putInt(SPKConstants.MY_LIMIT_NO_PASSWORD, my.data.limit_no_password)
                    SPHelper.putString(SPKConstants.MY_CELL, my.data.cell)
                    SPHelper.putString(SPKConstants.MEMBER_ID, my.data.member_id)
                    SPHelper.putString(SPKConstants.BASIC_INFO, result)
                    _result.postValue(true)
                }
            }, getAccessToken())
    }

    /**
     * 快速登录
     */
    fun fastLogin(aliAccessToken: String) {
        val request = LoginFastRequest(aliAccessToken)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_FAST_LOGIN, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    var resp = Gson().fromJson(result, LoginResponse::class.java)
                    SPHelper.saveLoginInfo(resp.data)
                    getMyInfo()
                }
            })
    }
}